package cn.edu.hust.cm.glideimagedemo;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.SeekBar;

import java.util.ArrayList;
import java.util.List;

public class ImageGridActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener, SeekBar.OnSeekBarChangeListener {

    public final static String[] IMAGE_SET = {
            "http://i.imgur.com/rFLNqWI.jpg",
            "http://i.imgur.com/C9pBVt7.jpg",
            "http://i.imgur.com/rT5vXE1.jpg",
            "http://i.imgur.com/aIy5R2k.jpg",
            "http://i.imgur.com/MoJs9pT.jpg",
            "http://i.imgur.com/S963yEM.jpg",
            "http://i.imgur.com/rLR2cyc.jpg",
            "http://i.imgur.com/SEPdUIx.jpg",
            "http://i.imgur.com/aC9OjaM.jpg",
            "http://i.imgur.com/76Jfv9b.jpg",
            "http://i.imgur.com/fUX7EIB.jpg",
            "http://i.imgur.com/syELajx.jpg",
            "http://i.imgur.com/COzBnru.jpg",
            "http://i.imgur.com/Z3QjilA.jpg",
            "http://campusx-10046755.image.myqcloud.com/9fdf2add-2fe0-47f5-9e97-08c38b017ca3",
            "http://campusx-10046755.image.myqcloud.com/75e82a9b-bcb1-476c-a514-4bc107a2e67c",
            "http://campusx-10046755.image.myqcloud.com/7a52c03c-c080-43e8-90cc-a26c458875ee",
            "http://campusx-10046755.image.myqcloud.com/96c8d0a8-2952-41cc-b808-adc298c0fdc4",
            "http://campusx-10046755.image.myqcloud.com/a5f5a696-2766-4338-9008-1dcbbd000e4c",
            "http://campusx-10046755.image.myqcloud.com/96c8d0a8-2952-41cc-b808-adc298c0fdc4",
            "http://campusx-10046755.image.myqcloud.com/44c5f696-c8b1-49e0-8ae4-57b356206c87",
            "http://campusx-10046755.image.myqcloud.com/a1f5c6e4-18a2-4838-915a-0bd0de11bb78",
            "http://campusx-10046755.image.myqcloud.com/6a169162-1b4d-403e-acb9-59348b45dc2d",
            "http://campusx-10046755.image.myqcloud.com/c1aace3f-5a6c-438d-b9f8-c62ce4c81694",
            "http://image57.360doc.com/DownloadImg/2012/12/1116/28841018_14.jpg",
            "http://img.pconline.com.cn/images/upload/upc/tx/wallpaper/1209/10/c1/13758581_1347257278695.jpg",
            "http://img04.tooopen.com/images/20121122/tooopen_201211221810434692.jpg",
            "http://pic49.nipic.com/file/20140926/17961491_122014331000_2.jpg",
            "http://image.tianjimedia.com/uploadImages/2013/284/F879R8LSSQ28.jpg",
            "http://pic45.nipic.com/20140730/12005913_100105395000_2.jpg",
            "http://pic62.nipic.com/file/20150303/17961491_092446597000_2.jpg",
            "http://pic48.nipic.com/file/20140909/17961491_082407720000_2.jpg",
            "http://img2.3lian.com/2014/c7/29/d/23.jpg",
            "http://image5.tuku.cn/pic/wallpaper/fengjing/langmandushiweimeisheyingkuanpingbizhi/006.jpg",
            "http://pic54.nipic.com/file/20141126/17961491_093029437000_2.jpg",
            "http://image72.360doc.com/DownloadImg/2014/05/0402/41292510_3.jpg",
            "http://h5.86.cc/walls/20150922/1024x768_0cce092c00c8dec.jpg",
            "http://pic41.nipic.com/20140531/1812578_155037556122_2.jpg",
            "http://a0.att.hudong.com/11/04/300224654811132504045236607_950.jpg",
            "http://pic51.nipic.com/file/20141023/17961491_140345410000_2.jpg",
            "http://t10.baidu.com/it/u=4043754453,763123347&fm=32&img.jpg",
            "http://pic65.nipic.com/file/20150421/9885883_114750128000_2.jpg",
            "http://pic38.nipic.com/20140305/15121266_203342815000_2.jpg"
    };

    private RecyclerView mRvGrid;
    private ImageAdapter mImageAdapter;
    private CheckBox mCbBlur;
    private CheckBox mCbRound;

    private int mCornerRadius;

    public static void start(Context context) {
        Intent starter = new Intent(context, ImageGridActivity.class);
        context.startActivity(starter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_grid);

        mRvGrid = (RecyclerView) findViewById(R.id.rv_grid);
        mRvGrid.setLayoutManager(new GridLayoutManager(this, 3));
        // NOTICE
        mRvGrid.getRecycledViewPool().setMaxRecycledViews(0, 20);
        mImageAdapter = new ImageAdapter();
        mRvGrid.setAdapter(mImageAdapter);

        for (String imageUrl : IMAGE_SET) {
            mImageAdapter.add(imageUrl);
        }
        mImageAdapter.notifyDataSetChanged();

        mCbBlur = (CheckBox) findViewById(R.id.cb_blur);
        mCbRound = (CheckBox) findViewById(R.id.cb_round);
        mCbBlur.setOnCheckedChangeListener(this);
        mCbRound.setOnCheckedChangeListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_image_grid, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.action_select_radius) {
            new SeekBarDialogFragment().show(getSupportFragmentManager(), "dialog");
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.cb_blur:
                setBlur(isChecked);
                break;

            case R.id.cb_round:
                setRound(isChecked);
                break;
        }
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        if (fromUser) {
            mCornerRadius = progress;
        }
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        setCornerRadius(mCornerRadius);
    }

    public void setBlur(boolean blur) {
        mImageAdapter.setBlur(blur);
    }

    public void setRound(boolean round) {
        mImageAdapter.setRound(round);
    }

    public void setCornerRadius(int cornerRadius) {
        mImageAdapter.setCornerRadius(cornerRadius);
    }

    private static class ImageAdapter extends RecyclerView.Adapter<Holder> {

        private List<String> mImageUrls;
        private boolean mBlur;
        private boolean mRound;
        private int mCornerRadius;

        public ImageAdapter() {
            mImageUrls = new ArrayList<>();
        }

        public void add(String imageUrl) {
            mImageUrls.add(imageUrl);
        }

        @Override
        public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new Holder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_image_grid, parent, false));
        }

        @Override
        public void onBindViewHolder(Holder holder, int position) {
            // call these methods before setImageUrl
            holder.ivImage.setBlur(mBlur);
            holder.ivImage.setRound(mRound);
            holder.ivImage.setCornerRadius(mCornerRadius);

            holder.ivImage.setImageUrl(mImageUrls.get(position));
        }

        @Override
        public int getItemCount() {
            return mImageUrls.size();
        }

        public void setBlur(boolean blur) {
            mBlur = blur;
            notifyDataSetChanged();
        }

        public void setRound(boolean round) {
            mRound = round;
            notifyDataSetChanged();
        }

        public void setCornerRadius(int cornerRadius) {
            mCornerRadius = cornerRadius;
            notifyDataSetChanged();
        }
    }

    private static class Holder extends RecyclerView.ViewHolder {

        private SquareImageView ivImage;

        public Holder(View itemView) {
            super(itemView);
            ivImage = (SquareImageView) itemView.findViewById(R.id.iv_image);
        }
    }
}
